<template>
  <div>
    <vol-box :lazy="true" :model.sync="model.box2" title="选择属性" :height="200" :width="500"
      :padding="15">
      <VolForm ref="myform" :loadKey="loadKey" :formFileds="formFileds" :formRules="formRules">
      </VolForm>
      <div slot="footer">
        <Button type="success" @click="getForm(val)">确定</Button><Button type="info"
          @click="model.box2 = false">
          <Icon type="md-close" />关闭
        </Button>
      </div>
    </vol-box>
  </div>
</template>
<script>
import VolForm from "@/components/basic/VolForm.vue";
import VolBox from "@/components/basic/VolBox.vue";
import VolTable from "@/components/basic/VolTable.vue";

export default {
  data() {
    return {
      // 选择属性有没有值
      attributeNum: "",

      val: "", //判断是产品还是套票
      specList: [],
      table1url: "",
      model: {
        box2: false,
        box1: false,
        //extend:extend,
        //columns:[{field:'',title:'订单号',type:'string',width:200,align:'left'},],
      },
      res: [],
      //tableData:[],
      loadKey: true,
      formFileds: {
        1111: [],
        1112: [],
        1113: [],
        1114: [],
        1115: [],
        1116: [],
        1117: [],
        1118: [],
        1119: [],
        // attr_fx: [],
        // attr_rq: [],
        // attr_dwj: [],
        // attr_jxrs: [],
        // attr_sd: [],
      },
      formRules: [
      ],
      show: true,
    };
  },
  components: { VolForm, "vol-box": VolBox, VolTable },
  methods: {
    addrows(item) {
      this.$refs.tables.addRow(item);
    },
    getformRules() {
      let that = this.$store.getters.data().reportPrice;

      this.http
        .get(
          "api/Products/GetProductType?id=" +
            that.editFormFileds.type +
            "&supplierCode=" +
            that.editFormFileds.supplierCode,
          ""
        )
        .then((reslut) => {
          // console.log("1111111",reslut);
          this.attributeNum = reslut.data.length;
          if (reslut.data.length != 0) {
            if (reslut.status) {
              this.formRules = [];
              reslut.data.forEach((item) => {
                item.data.forEach((e) => {
                  e.disabled = false;
                });
                console.log("item", item);
                item.onChange = (val, option) => {
                  console.log("change", val, option);
                  if (!val.length) {
                    option.data.forEach((ele) => {
                      ele.disabled = false;
                    });
                    return;
                  }
                  let t = option.data.filter((it) => it.key == val[0])[0].group;
                  option.data.forEach((ele) => {
                    if (t != ele.group) {
                      ele.disabled = true;
                    }
                  });
                  // console.log(this.formFileds[item.field]);
                };
              });
              this.formRules.push(reslut.data);
            }
          }
        });
      console.log("进入1", this.formRules);
    },
    getForm(val) {
      // console.log(val,typeof(val)==='undefined')
      let that = this.$store.getters.data().reportPrice;
      let params = {
        delKeys: null,
        detailData: null,
        mainData: {},
      };
      this.formRules.forEach((x) => {
        x.forEach((item) => {
          params.mainData[item.field] = this.formFileds[item.field];
          // console.log(
          //   "cc",
          //   item.field,
          //   params.mainData[item.field],
          //   this.formFileds[item.field]
          // );
        });
      });
      let a = JSON.stringify(params.mainData)
      that.editFormFileds.attributes = a.replace(new RegExp("\\\\\"","gm"),"\"")
      let AddGoods = that.editFormFileds.name;
      if (that.editFormFileds.type[0] == "JiuDian") {
        let hotel_ID = that.editFormFileds.hotel_ID;
        let pmsRoomCode = that.editFormFileds.pmsRoomCode;
        params.mainData = {
          ...params.mainData,
          AddGoods,
          hotel_ID,
          pmsRoomCode,
        };
      } else {
        params.mainData = { ...params.mainData, AddGoods };
      }

        this.http.post("/api/Products/AddGoods", params).then((res) => {
          // console.log("数据", res);
          if (res.status) {
            res.data.forEach((item) => {
              item.originalPrice = that.editFormFileds.costprice
                ? that.editFormFileds.costprice
                : 0;
            });
            console.log("tag", that.editFormFileds.type[0]);
            // 修改影院无法生成价格日历问题
            if (that.editFormFileds.type[0] == "YingYuan") {
              res.data.forEach((item) => {
                item.name = item.name + (item.attributes ? '(' + item.attributes + ')' : '')
                item.attributesName = item.attributes
                item.attributes = null;
                item.spare1 = null;
              });
              that.$refs.detail.reset();
              that.$refs.detail.rowData.push(...res.data);
              this.model.box2 = false;
              return;
            }
            if (typeof val === "undefined") {
              res.data.forEach((item) => {
                item.name = item.name + (item.attributes ? '(' + item.attributes + ')' : '')
              });

              //if (val == "ChanPing") {
              that.$refs.detail.reset();
              that.$refs.detail.rowData.push(...res.data);
            } else if (val == "TiaoPiao") {
              console.log("tag", res.data);
              res.data.forEach(item => {
                item.code = ''
                item.name = item.name + (item.attributes ? '(' + item.attributes + ')' : '')
              })
              that.$refs.modelFooter.tableData = [];
              that.$refs.modelFooter.tableData.push(...res.data);
            }
            //  this.$refs.detail.addRow(res.data);
            // that.$refs.detail.$refs.table = res.data;
            this.model.box2 = false;
            // this.$refs.myform.reset();
          }
        });
    },
    reset() {
      if (Object.keys(this.$refs).length) {
        // console.log("进入");+
        for (let ele in this.formFileds) {
          this.formFileds[ele] = [];
        }
        this.formRules = [];
      }
    },
    open(val) {
      this.val = val;
      //this.init();
      this.getformRules();
      this.model.box2 = true;
      //this.getinitDictionary();
    },
  },


};
</script>
